<template>
    <div class="search">
        <!-- 顶部导航 -->
        <van-nav-bar fixed title="商品列表" left-arrow @click-left="$router.go(-1)" />


        <!-- 搜索框 -->
        <!-- @click="$router.push('/search')" -->
        <van-search readonly shape="round" background="#ffffff" :value="querySearch" show-action >
            <template #action>
                <van-icon class="tool" name="apps-o" />
            </template>
        </van-search>

        <!-- 排序选项按钮 -->
        <div class="sort-btns">
            <div class="sort-item" v-for="item in options" :key="item.value" @click="clickHandle(item.value)">{{ item.label }}</div>
        </div>


        <!-- 商品列表 -->
        <div class="goods-list">
            <GoodsItem v-for="item in goodsList" :key="item.goods_id" :item="item"></GoodsItem>
        </div>
    </div>
</template>

<script>
import GoodsItem from '@/components/GoodsItem/index.vue'
import { getShopSearch } from '@/api/product/index.js'
export default {
    name: 'SearchIndex',
    components: {
        GoodsItem
    },
    data () {
        return {
            // 商品列表
            goodsList: [],
            // 当前页数
            page: 1,
            // 总数
            total: 0,
            // 顶部排序菜单
            options: [
                {
                    value: 'all',
                    label:'综合'
                },
                 {
                    value: 'sales',
                    label: '销量'
                },
                 {
                    value: 'price',
                    label: '价格'
                }
            ],
            // 排序字段
            sortType:'all'
        }
    },
    computed: {
        // 搜索关键子
        querySearch() { 
            return this.$route.query.search
        },

        // 商品分类id
        shopCateGoryId() { 
            return this.$route.query.categoryId
        }
    },
    created() { 
        this.getShopSearch()
    },
    methods: {
        // 获取商品搜索
        async getShopSearch() { 
            this.goodsList = []
            const params = {
                categoryId: this.shopCateGoryId,
                goodsName: this.querySearch,
                page: this.page,
                sortType:this.sortType
            }
            const { data: { list } } = await getShopSearch(params)
            this.goodsList = list.data || []
            this.total = list.total || 0
        },

        // 排序
        clickHandle(key) {
            this.sortType = key
            this.getShopSearch()
        }
    }
}
</script>

<style lang="less" scoped>
.search {
    padding-top: 46px;

    ::v-deep .van-icon-arrow-left {
        color: #333;
    }

    .tool {
        font-size: 24px;
        height: 40px;
        line-height: 40px;
    }

    .sort-btns {
        display: flex;
        height: 36px;
        line-height: 36px;

        .sort-item {
            text-align: center;
            flex: 1;
            font-size: 16px;
        }
    }
}

// 商品样式
.goods-list {
    background-color: #f6f6f6;
}
</style>